/**
 * Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.service.impl;

import java.util.List;

import javax.inject.Inject;

import org.apache.commons.beanutils.BeanUtils;

import ca.ioniq.server.dao.ItemDAO;
import ca.ioniq.server.dao.PromotionDAO;
import ca.ioniq.server.service.MarketingService;
import ca.ioniq.server.service.exception.PromotionException;
import ca.ioniq.shared.entity.Item;
import ca.ioniq.shared.entity.Promotion;

import com.google.inject.persist.Transactional;

public class MarketingServiceImpl implements MarketingService {
	
	private final PromotionDAO promotionDao;
	private final ItemDAO itemDao;
	
	@Inject
	public MarketingServiceImpl(PromotionDAO promotionDao, ItemDAO itemDao) {
		this.promotionDao = promotionDao;
		this.itemDao = itemDao;
	}

	@Transactional
	public Promotion viewPromotion(Long promotionId) {
		 return promotionDao.findById(promotionId);
	}
	
	@Transactional
	public Promotion createPromotion(Promotion promotion) {
		 promotionDao.persist(promotion);
		 return promotion;
	}
	
	@Transactional
	public void removePromotion(Long promotionId) throws PromotionException {
		try {
			Promotion currentPromotion = promotionDao.findById(promotionId);
			promotionDao.delete(currentPromotion);
		} catch (Exception e) {
			throw new PromotionException("Cannot remove this promotion; maybe this promotion is not present in repository", e);
		}
		
	}

	@Transactional
	public void updatePromotion(Promotion promotion) throws PromotionException {
		try {			
			Promotion currentPromotion = promotionDao.findById(promotion.getId());
			Long currentPromotionId = currentPromotion.getId();
			BeanUtils.copyProperties(currentPromotion, promotion);
			currentPromotion.setId(currentPromotionId);
			promotionDao.update(currentPromotion);
		} catch (Exception e) {
			throw new PromotionException("Cannot update this promotion; maybe this promotion is not present in repository", e);
		}
	}

	@Transactional
	public void addNewItemsToPromotion(Long promotionId, List<Item> items) {
		Promotion promotion = promotionDao.findById(promotionId);
		for (Item item : items) {
            // the line below is causing an error
			promotion.getItems().add(item);
			itemDao.merge(item);
		}
		promotionDao.merge(promotion);
	}

	@Transactional
	public void addExistingItemsToPromotion(Long promotionId, List<Item> items) {
		Promotion promotion = promotionDao.findById(promotionId);
		for (Item item : items) {
            // the line below is also causing a compilation error
			promotion.getItems().add(item);
			itemDao.merge(item);
		}
		promotionDao.merge(promotion);
	}

}
